package com.tbit.uqbike.service.business.impl;

import com.tbit.uqbike.constant.ReturnPosLogConstant;
import com.tbit.uqbike.object.business.Point;
import com.tbit.uqbike.object.pojo.BorrowPosLog;
import com.tbit.uqbike.object.pojo.PosLog;
import com.tbit.uqbike.object.pojo.ReturnPosLog;
import com.tbit.uqbike.service.base.CalibrateService;
import com.tbit.uqbike.service.business.ReturnPosLogService;
import com.tbit.uqbike.webmanager.dao.log.BorrowPosLogDao;
import com.tbit.uqbike.webmanager.dao.log.ReturnPosLogDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Author: chen
 * @Description: 用户还车手机位置车辆位置
 * @Date: 创建于 14:50 2020/6/5
 */
@Service
public class ReturnPosLogServiceImpl implements ReturnPosLogService {
    @Autowired
    private ReturnPosLogDao returnPosLogDao;
    @Autowired
    private CalibrateService calibrateService;
    @Autowired
    private BorrowPosLogDao borrowPosLogDao;

    @Override
    public PosLog queryPage(String orderNO, String userCode, String machineNO, Integer mapType) {
        Map<String, Object> map = new HashMap<>();
        map.put("orderNO", orderNO);
        map.put("userCode", userCode);
        map.put("machineNO", machineNO);
        List<ReturnPosLog> list = returnPosLogDao.queryPage(map);
        BorrowPosLog borrowPosLog = borrowPosLogDao.selectByOrderNo(orderNO);
        PosLog posLog = new PosLog();
        if (Objects.nonNull(borrowPosLog)) {
            Point point = calibrateService.calibrateOffSet(borrowPosLog.getLon(), borrowPosLog.getLat(), mapType);
            PosLog.BorrowPosInfo borrowPosInfo = PosLog.BorrowPosInfo
                    .builder()
                    .posTime(borrowPosLog.getPosTime())
                    .lon(point.getX())
                    .lat(point.getY())
                    .posFrom(borrowPosLog.getPosFrom())
                    .build();
            posLog.setBorrowPosInfo(borrowPosInfo);
        }
        if (list.size() > 0) {
            PosLog.ReturnPosInfo.ReturnPosInfoBuilder builder = PosLog.ReturnPosInfo.builder();

            for (ReturnPosLog r : list) {
                if (r.getPosFrom().equals(ReturnPosLogConstant.TYPE_MACHINE)) {
                    posLog.setOrderNO(r.getOrderNO());
                    posLog.setMachineNO(r.getMachineNO());
                    posLog.setUserCode(r.getUserCode());
                    Point point = calibrateService.calibrateOffSet(r.getLon(), r.getLat(), mapType);
                    builder.machineLon(point.getX())
                            .machineLat(point.getY())
                            .machineTime(r.getPosTime());
                } else if (r.getPosFrom().equals(ReturnPosLogConstant.TYPE_PGONE)) {
                    Point point = calibrateService.calibrateOffSet(r.getLon(), r.getLat(), mapType);
                    builder.phoneLon(point.getX())
                            .phoneLat(point.getY())
                            .phoneTime(r.getPosTime());
                }
            }
            posLog.setReturnPosInfo(builder.build());
        }
        return posLog;
    }

    @Override
    public Integer queryCount(String orderNO, String userCode, String machineNO) {
        Map<String, Object> map = new HashMap<>();
        map.put("orderNO", orderNO);
        map.put("userCode", userCode);
        map.put("machineNO", machineNO);
        return returnPosLogDao.queryCount(map);
    }
}
